package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.action.front.param.OrderQueryParam;
import com.qifan.alb2b.domain.OrderInfoDomain;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.DateUtil;
import com.qifan.alb2b.util.Page;

@Repository
public class OrderInfoDao extends BaseDao{
	private static final Log logger=LogFactory.getLog(OrderInfoDao.class);
	
	public Page query(OrderQueryParam oqp,DataGridModel dgm){
		logger.debug("查询订单");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select t0.* from orderInfo t0,userInfo t1 where 1=1 and t0.userId=t1.userId");
		if(oqp.getOrderId()!=null&&!"".equals(oqp.getOrderId())){
			sql.append(" and t0.orderId=?");
			params.add(oqp.getOrderId());
		}
		if(oqp.getUserId()!=null&&!"".equals(oqp.getUserId())){
			sql.append(" and t0.userId=?");
			params.add(oqp.getUserId());
		}
		if(oqp.getUserName()!=null&&!"".equals(oqp.getUserName())){
			sql.append(" and t1.userName like ?");
			params.add("%"+oqp.getUserName()+"%");
		}
		if(oqp.getGroupType()!=-1){
			sql.append(" and t0.groupType=?");
			params.add(oqp.getGroupType());
		}
		if(oqp.getOrderStatus()!=-1){
			sql.append(" and t0.orderStatus=?");
			params.add(oqp.getOrderStatus());
		}
		if(oqp.getRouteId()!=null&&!"".equals(oqp.getRouteId())){
			sql.append(" and t0.routeId=?");
			params.add(oqp.getRouteId());
		}
		if(oqp.getCreateBeginTime()!=null){
			sql.append(" and t0.createTime>?");
			params.add(DateUtil.ymd(DateUtil.previous(oqp.getCreateBeginTime(), 1)));
		}
		if(oqp.getCreateBeginTime()!=null){
			sql.append(" and t0.createTime<?");
			params.add(DateUtil.ymd(DateUtil.next(oqp.getCreateEndTime(), 1)));
		}
		
		//判断是否排序
		if(dgm.getSort()!=null&&!"".equals(dgm.getSort()))
			sql.append(" order by ").append("t0."+dgm.getSort()).append(" ").append(dgm.getOrder());
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, OrderInfoDomain.class);
	}
	
	/**
	 * 根据当前时间查询到期的未付款的订单
	 * @param now
	 * @return
	 */
	public List<Map<String, Object>> queryOrder(Date now){
		StringBuilder sql=new StringBuilder();
		sql.append("select t0.*,t1.userName,t1.userCall,t1.email from orderinfo t0 left join userinfo t1 on t0.userid=t1.userid where");
		sql.append(" t0.orderStatus=").append(Constants.ORDER_STATUS_NOPAY);
		sql.append(" and payTime<=?");
		return getJdbcTemplate().queryForList(sql.toString(), DateUtil.format(now, "yyyy-MM-dd HH:mm:ss"));
	}
	
	public OrderInfoDomain getOrderById(String orderId){
		String sql="select * from orderInfo where orderId=?";
		List<OrderInfoDomain> list=JdbcUtil.queryForList(this, OrderInfoDomain.class, sql, orderId);
		if(list.size()==0)
			return null;
		return list.get(0);
	}
	
	public void add(OrderInfoDomain orderInfo){
		super.addObj(orderInfo);
	}
	
	/**
	 * 更新订单状态
	 * @param orderId
	 * @param orderStatus
	 */
	public void updateStatus(String orderId,int orderStatus){
		String sql="update orderInfo set orderStatus=? where orderId=?";
		this.getJdbcTemplate().update(sql, new Object[]{orderStatus,orderId});
	}
	
}
